﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using System.ComponentModel;
using System.Threading;

namespace WPF_JumpList
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();

			taskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
			_backgroundWorker = new BackgroundWorker();
			_backgroundWorker.WorkerSupportsCancellation = true;
			_backgroundWorker.WorkerReportsProgress = true;
			_backgroundWorker.DoWork += BackgroundWorkerDoWork;
			_backgroundWorker.ProgressChanged += (s, e) => taskbarItemInfo.ProgressValue = (double)e.ProgressPercentage / 100;
		}

		private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
		{
			for (int i = 0; (i < 100) && (_backgroundWorker.CancellationPending == false); ++i)
			{
				Thread.Sleep(25);
				_backgroundWorker.ReportProgress(i);
			}
		}

		private void AddCalculator(object sender, RoutedEventArgs e)
		{
			JumpTask jumpTask = new JumpTask();
			jumpTask.ApplicationPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "calc.exe");
			jumpTask.IconResourcePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "calc.exe");
			jumpTask.Title = "Calculator";
			jumpTask.Description = "Open Calculator";

			JumpList jumpList = JumpList.GetJumpList(App.Current);
			jumpList.JumpItems.Add(jumpTask);
			jumpList.Apply();
		}

		private void ClearJumpList(object sender, RoutedEventArgs e)
		{
			JumpList jumpList = JumpList.GetJumpList(App.Current);
			jumpList.JumpItems.Clear();
			jumpList.Apply();
		}

		private BackgroundWorker _backgroundWorker = null;

		private void StartWorker(object sender, RoutedEventArgs e)
		{
			if (_backgroundWorker.IsBusy == false)
			{
				_backgroundWorker.RunWorkerAsync();
			}
		}

		private void StopWorker(object sender, RoutedEventArgs e)
		{
			_backgroundWorker.CancelAsync();
		}
	}
}
